﻿namespace In66.Net.Repository.EFCoreData
{
    public class BaseRepository<TEntity> : AbstractBaseRepository<DbContext, TEntity>, IBaseRepository<TEntity>
           where TEntity : Entity, IEntity<long>
    {
        private readonly IAdoExecuterWithQuerierRepository? _adoExecuteQuerier;

        public BaseRepository(DbContext dbContext, IAdoExecuterWithQuerierRepository? adoExecuteQuerier = null)
            : base(dbContext)
            => _adoExecuteQuerier = adoExecuteQuerier;

        public IAdoQuerierRepository? AdoQuerier
        {
            get
            {
                if (_adoExecuteQuerier is null)
                {
                    return null;
                }

                if (!_adoExecuteQuerier.HasDbConnection())
                {
                    _adoExecuteQuerier.ChangeOrSetDbConnection(DbContext.Database.GetDbConnection());
                }

                return _adoExecuteQuerier;
            }
        }
    }
}
